//
//  SHTabBarButton.m
//  ShinyOrder
//
//  Created by 王方伟 on 16/7/22.
//  Copyright © 2016年 ShinyHub. All rights reserved.
//

#import "SHTabBarButton.h"
#import "SHBadgeButton.h"

// 图标的比例
#define TabBarButtonImageRatio 0.6

@interface SHTabBarButton ()
@property (nonatomic, weak) SHBadgeButton *badgeButton;
@end

@implementation SHTabBarButton
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setButtonStyle];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if (self) {
        [self setButtonStyle];
    }
    return self;
}

#pragma mark 设置Buton的属性
-(void)setButtonStyle{
    //设置图片居中
    self.imageView.contentMode=UIViewContentModeCenter;
    //设置文字居中
    self.titleLabel.textAlignment=NSTextAlignmentCenter;
    //设置文字大小
    self.titleLabel.font = [UIFont systemFontOfSize:10];
    //设置文字颜色
    [self setTitleColor:SHColor(60, 79, 94) forState:UIControlStateNormal];
    //设置选中时的颜色
    [self setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];
    
    //设置提醒按钮
    SHBadgeButton *badgeButton=[[SHBadgeButton alloc]init];
    badgeButton.x = LineX(50);
    badgeButton.y = 0;
    [self addSubview:badgeButton];
    self.badgeButton=badgeButton;
}


- (void)layoutSubviews{
    [super layoutSubviews];
}

-(void)setItem:(UITabBarItem *)item{
    _item=item;
    // KVO 监听属性改变
    [item addObserver:self forKeyPath:@"badgeValue" options:0 context:nil];
    [item addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
    [item addObserver:self forKeyPath:@"image" options:0 context:nil];
    [item addObserver:self forKeyPath:@"selectedImage" options:0 context:nil];
    [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];
}

/**
 *  监听到某个对象的属性改变了,就会调用
 *
 *  @param keyPath 属性名
 *  @param object  哪个对象的属性被改变
 *  @param change  属性发生的改变
 */
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    [self setTitle:self.item.title forState:UIControlStateNormal];
    [self setImage:self.item.image forState:UIControlStateNormal];
    [self setImage:self.item.selectedImage forState:UIControlStateSelected];
    //设置提示按钮的值
    [self.badgeButton badgeValue:self.item.badgeValue finishBlock:^{
        self.item.badgeValue = 0;
    }];
}

#pragma mark 覆盖按钮的高亮状态
-(void)setHighlighted:(BOOL)highlighted{
}

#pragma mark 设置Button内部的图片
-(CGRect)imageRectForContentRect:(CGRect)contentRect{
    CGFloat imgX=0;
    CGFloat imaY=0;
    CGFloat imgH=contentRect.size.height*TabBarButtonImageRatio;
    CGFloat imgW=contentRect.size.width;
    return CGRectMake(imgX, imaY, imgW, imgH);
}

#pragma mark 设置Button内部的文字
-(CGRect)titleRectForContentRect:(CGRect)contentRect{
    CGFloat titX=0;
    CGFloat titY=contentRect.size.height*TabBarButtonImageRatio;
    CGFloat titH=contentRect.size.height-titY;
    CGFloat titW=contentRect.size.width;
    return CGRectMake(titX, titY, titW, titH);
}

-(void)dealloc{
    [self.item removeObserver:self forKeyPath:@"badgeValue"];
    [self.item removeObserver:self forKeyPath:@"title"];
    [self.item removeObserver:self forKeyPath:@"image"];
    [self.item removeObserver:self forKeyPath:@"selectedImage"];
}
@end
